{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Remove One Element to Make the Array Strictly Increasing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: canBeIncreasing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #删除一个元素使数组严格递增"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ，如果 <strong>恰好</strong> 删除 <strong>一个</strong> 元素后，数组 <strong>严格递增</strong> ，那么请你返回 <code>true</code> ，否则返回 <code>false</code> 。如果数组本身已经是严格递增的，请你也返回 <code>true</code> 。</p>\n",
    "\n",
    "<p>数组 <code>nums</code> 是 <strong>严格递增</strong> 的定义为：对于任意下标的 <code>1 &lt;= i &lt; nums.length</code> 都满足 <code>nums[i - 1] &lt; nums[i]</code> 。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [1,2,<strong>10</strong>,5,7]\n",
    "<b>输出：</b>true\n",
    "<b>解释：</b>从 nums 中删除下标 2 处的 10 ，得到 [1,2,5,7] 。\n",
    "[1,2,5,7] 是严格递增的，所以返回 true 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [2,3,1,2]\n",
    "<b>输出：</b>false\n",
    "<b>解释：</b>\n",
    "[3,1,2] 是删除下标 0 处元素后得到的结果。\n",
    "[2,1,2] 是删除下标 1 处元素后得到的结果。\n",
    "[2,3,2] 是删除下标 2 处元素后得到的结果。\n",
    "[2,3,1] 是删除下标 3 处元素后得到的结果。\n",
    "没有任何结果数组是严格递增的，所以返回 false 。</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [1,1,1]\n",
    "<b>输出：</b>false\n",
    "<b>解释：</b>删除任意元素后的结果都是 [1,1] 。\n",
    "[1,1] 不是严格递增的，所以返回 false 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [1,2,3]\n",
    "<b>输出：</b>true\n",
    "<b>解释：</b>[1,2,3] 已经是严格递增的，所以返回 true 。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= nums.length &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [remove-one-element-to-make-the-array-strictly-increasing](https://leetcode.cn/problems/remove-one-element-to-make-the-array-strictly-increasing/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [remove-one-element-to-make-the-array-strictly-increasing](https://leetcode.cn/problems/remove-one-element-to-make-the-array-strictly-increasing/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,10,5,7]', '[2,3,1,2]', '[1,1,1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        # 检查数组 nums 在删去下标为 idx 的元素后是否严格递增\n",
    "        def check(idx: int) -> bool:\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            # 寻找非递增相邻下标对\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        k = 0\n",
    "        for i in range(0, len(nums)-1):\n",
    "            if nums[i] >= nums[i+1]:\n",
    "                if k == 0:\n",
    "                    k = 1\n",
    "                    try:\n",
    "                        if i-1 >= 0:\n",
    "                            if (nums[i-1] < nums[i+1] or nums[i] < nums[i+2]):\n",
    "                                continue\n",
    "                            else:\n",
    "                                return False\n",
    "                    except:\n",
    "                        return True\n",
    "                else:\n",
    "                    print(2)\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        # 检查数组 nums 在删去下标为 idx 的元素后是否严格递增\n",
    "        def check(idx: int) -> bool:\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            # 寻找非递增相邻下标对\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        for i in range(1, len(nums)-1):\n",
    "            wow = nums\n",
    "            if wow[i-1] >= wow[i]:\n",
    "                if wow[i-1] >= wow[i+1]:\n",
    "                    wow.pop(i-1)\n",
    "                    for j in range(1, len(wow)):\n",
    "                        if len(wow) > 1:\n",
    "                            if wow[j-1] >= wow[j]:\n",
    "                                return False\n",
    "                                break\n",
    "                else:\n",
    "                    wow.pop(i)\n",
    "                    for j in range(1, len(wow)):\n",
    "                        if len(wow) > 1:\n",
    "                            if wow[j-1] >= wow[j]:\n",
    "                                return False\n",
    "                                break\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        prepre = 0\n",
    "        pre = 0\n",
    "        cnt = 0\n",
    "        for num in nums:\n",
    "            if pre >= num:\n",
    "                # min\n",
    "                if prepre < num:\n",
    "                    pre = num\n",
    "                    \n",
    "                cnt += 1\n",
    "                if cnt > 1:\n",
    "                    return False\n",
    "            else:\n",
    "                prepre, pre = pre, num\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        ans = 0\n",
    "        if len(nums)<=2:\n",
    "            return True\n",
    "        if len(nums)==3:\n",
    "            if nums[1]>nums[0] or nums[2]>nums[1] or nums[2]>nums[0]:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        if len(nums)>3:\n",
    "            for i in range(1,len(nums)-2):\n",
    "                if nums[i]>=nums[i+1]:\n",
    "                    if nums[i-1]<nums[i+1] or nums[i]<nums[i+2]:\n",
    "                        ans+=1\n",
    "                    else:\n",
    "                        return False\n",
    "            i = len(nums)-2\n",
    "            if nums[i]>=nums[i+1]:\n",
    "                ans+=1\n",
    "            \n",
    "            if nums[0]>=nums[1]:\n",
    "                ans+=1\n",
    "\n",
    "        return True if ans<2 else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        def check(idx):\n",
    "            for i in range(1,n-1):\n",
    "                prev,curr = i-1,i\n",
    "                if prev>=idx:prev += 1\n",
    "                if curr>=idx:curr += 1\n",
    "                if nums[curr]<=nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "        for i in range(1,n):\n",
    "            if nums[i]<=nums[i-1]:\n",
    "                return check(i) or check(i-1)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n= len(nums)\n",
    "        def check(idx:int) -> bool:\n",
    "            for i in range(1,n-1):\n",
    "                prev, curr = i-1,i\n",
    "                if prev >= idx:\n",
    "                    prev+=1\n",
    "                if curr >= idx:\n",
    "                    curr +=1\n",
    "                if nums[curr]<=nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        for i in range(1,n):\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i-1)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        for i in range(len(nums)):\n",
    "            if sorted(nums[:i]+nums[i+1:])==nums[:i]+nums[i+1:]:\n",
    "                if len(set(nums[:i]+nums[i+1:]))==len(nums)-1:\n",
    "                 return True\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        if len(nums)==2:\n",
    "            return True\n",
    "        counter = 0\n",
    "        idx = None\n",
    "        for x in range(len(nums)-1):\n",
    "            if nums[x] >= nums[x + 1]:\n",
    "                counter += 1\n",
    "                if counter==2:\n",
    "                    return False\n",
    "                idx = x\n",
    "\n",
    "        if idx is None:\n",
    "            return True\n",
    "\n",
    "        if idx == 0 or idx == len(nums) - 2:\n",
    "            return True\n",
    "\n",
    "        if nums[idx - 1] < nums[idx + 1] or nums[idx]<nums[idx+2]:\n",
    "            return True\n",
    "\n",
    "        return False\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n=len(nums)\n",
    "        for i in range(n):\n",
    "            new=nums[:i]+nums[i+1:]\n",
    "            for j in range(1,n-1):\n",
    "                if new[j-1]>=new[j]: break\n",
    "            else: return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        # 检查数组 nums 在删去下标为 idx 的元素后是否严格递增\n",
    "        def check(idx: int) -> bool:\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            # 寻找非递增相邻下标对\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        if n == len(set(nums)) and nums == sorted(nums):\n",
    "            return True\n",
    "        for i in range(n):\n",
    "            arr = nums[:i]\n",
    "            arr.extend(nums[i+1:])\n",
    "            flag = True\n",
    "            for i in range(n-2):\n",
    "                if arr[i] >= arr[i+1]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag:\n",
    "                return True\n",
    "        \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        # 检查数组 nums 在删去下标为 idx 的元素后是否严格递增\n",
    "        def check(idx: int) -> bool:\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            # 寻找非递增相邻下标对\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        i, j = 0, 1\n",
    "        c = 0\n",
    "        while j < len(nums):\n",
    "            if nums[j] > nums[i]:\n",
    "                i = j\n",
    "                j += 1\n",
    "            else:\n",
    "                c += 1\n",
    "                if c > 1:\n",
    "                    return False\n",
    "                if i > 0:\n",
    "                    if nums[j] > nums[i - 1]:\n",
    "                        i -= 1\n",
    "                    else:\n",
    "                        j += 1\n",
    "                else:\n",
    "                    i = j\n",
    "                    j += 1\n",
    "        return c <= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        idx = None\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i+1] <= nums[i]:\n",
    "                idx = i\n",
    "                break\n",
    "        if idx is None:\n",
    "            return True\n",
    "        nums1 = nums[:idx] + nums[idx+1:]\n",
    "        nums2 = nums[:idx+1] + nums[idx+2:]\n",
    "        ans1 = ans2 = True\n",
    "        for i in range(len(nums1)-1):\n",
    "            if nums1[i+1] <= nums1[i]:\n",
    "                ans1 = False\n",
    "            if nums2[i+1] <= nums2[i]:\n",
    "                ans2 = False\n",
    "        return ans1 or ans2\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        def isIncerase(list):\n",
    "            num = 0\n",
    "            if list:\n",
    "                if len(list)>2:\n",
    "                    for i in range(len(list)-1):\n",
    "                        if list[i]>=list[i+1]:\n",
    "                            if num>=1 :\n",
    "                                return False\n",
    "                            else:\n",
    "                                num += 1\n",
    "                                if i>0 and i+1<len(list) and list[i-1] < list[i+1]:# 删除自己\n",
    "                                    continue\n",
    "                                elif i+2 <len(list) and list[i]<list[i+2]:# 删除下一个\n",
    "                                    continue\n",
    "                                elif i==0 or i == len(list)-2:# 当是第一个元素是，删除自己；当是倒数第二时，删除下一个\n",
    "                                    continue\n",
    "                                else:\n",
    "                                    return False\n",
    "            return True\n",
    "        \n",
    "        if isIncerase(nums):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        l = len(nums)\n",
    "        a = 0\n",
    "        for i in range(l-1):\n",
    "            if nums[i] >= nums[i+1]:\n",
    "                if 1 <= i < l-1:\n",
    "                   if nums[i-1] < nums [i+1]:\n",
    "                       del nums[i]\n",
    "                       break\n",
    "                   elif nums[i -1] >= nums[i+1]:\n",
    "                       del nums[i+1]\n",
    "                       break\n",
    "                else:\n",
    "                    del nums[i]\n",
    "                    break\n",
    "        l2 = len(nums)\n",
    "        for s in range(l2-1):\n",
    "            if nums[s] < nums[s+1]:\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        # 检查数组 nums 在删去下标为 idx 的元素后是否严格递增\n",
    "        def check(idx: int) -> bool:\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            # 寻找非递增相邻下标对\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        # 检查数组 nums 在删去下标为 idx 的元素后是否严格递增\n",
    "        def check(idx: int) -> bool:\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            # 寻找非递增相邻下标对\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        def check(l):\n",
    "            pre = l[0]\n",
    "            for i in range(1,len(l)):\n",
    "                if l[i]<=pre:\n",
    "                    return False\n",
    "                pre = l[i]\n",
    "            return True\n",
    "\n",
    "        pre = nums[0]\n",
    "        for i in range(1,len(nums)):\n",
    "            if nums[i]<=pre:\n",
    "                res1 = nums[:i]+nums[i+1:]\n",
    "                res2 = nums[:i-1]+nums[i:]\n",
    "                if check(res1) or check(res2):\n",
    "                    return True\n",
    "                else:\n",
    "                    return False\n",
    "            pre = nums[i]\n",
    "        return True\n",
    "\n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        def check(idx):\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        for i in range(1, n):\n",
    "            if nums[i] <= nums[i - 1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        # n = len(nums)\n",
    "        # def check(idx):\n",
    "        #     for i in range(1, n-1):\n",
    "        #         prev, curr = i-1, i\n",
    "        #         if prev >= idx:\n",
    "        #             prev += 1\n",
    "        #         if curr >= idx:\n",
    "        #             curr += 1\n",
    "        #         if nums[curr] <= nums[prev]:\n",
    "        #             return False\n",
    "        #     return True\n",
    "\n",
    "        # for i in range(1,n):\n",
    "        #     if nums[i] <= nums[i-1]:\n",
    "        #         return check(i) or check(i-1)\n",
    "        # return True\n",
    "\n",
    "        left, right = 0, len(nums) - 1\n",
    "        while left<right:\n",
    "            if nums[left] < nums[left+1]:\n",
    "                left +=1\n",
    "            elif nums[right] > nums[right-1]:\n",
    "                right -= 1\n",
    "            else:\n",
    "                break\n",
    "        \n",
    "        return left == right or (right == left + 1 and (left == 0 or right == len(nums)-1 or nums[right] > nums[left-1] or nums[left] < nums[right + 1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        # 检查数组 nums 在删去下标为 idx 的元素后是否严格递增\n",
    "        def check(idx: int) -> bool:\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            # 寻找非递增相邻下标对\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        # 检查数组 nums 在删去下标为 idx 的元素后是否严格递增\n",
    "        def check(idx: int) -> bool:\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            # 寻找非递增相邻下标对\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        # 检查数组 nums 在删去下标为 idx 的元素后是否严格递增\n",
    "        def check(idx: int) -> bool:\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            # 寻找非递增相邻下标对\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "\n",
    "        def check(idx):\n",
    "            nums_copy = nums[:]\n",
    "            del nums_copy[idx]\n",
    "            for i in range(len(nums_copy)-1):\n",
    "                if nums_copy[i] >= nums_copy[i+1]:\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "        for i in range(1,n):\n",
    "            if nums[i-1] >= nums[i]:\n",
    "                return check(i) or check(i-1) \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n=len(nums)\n",
    "        for i in range(n):\n",
    "            nums1=nums[:]\n",
    "            del nums1[i]\n",
    "            nums2=sorted(nums1)\n",
    "            if nums2==nums1 and len(set(nums2))==len(nums2):\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        if len(nums)==2:\n",
    "            return True\n",
    "        counter = 0\n",
    "        idx = None\n",
    "        for x in range(len(nums)-1):\n",
    "            if nums[x] >= nums[x + 1]:\n",
    "                counter += 1\n",
    "                if counter==2:\n",
    "                    return False\n",
    "                idx = x\n",
    "\n",
    "        if idx is None:\n",
    "            return True\n",
    "\n",
    "        if idx == 0 or idx == len(nums) - 2:\n",
    "            return True\n",
    "\n",
    "        if nums[idx - 1] < nums[idx + 1] or nums[idx]<nums[idx+2]:\n",
    "            return True\n",
    "\n",
    "        return False\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        def check(s):\n",
    "            pre = -inf\n",
    "            for x in s:\n",
    "                if x <= pre:\n",
    "                    return False\n",
    "                pre = x\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i - 1] >= nums[i]:\n",
    "                return check(nums[:i-1]+nums[i:]) or check(nums[:i]+nums[i+1:])\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findAndDelete(self, cnt: int, nums: List[int]) -> bool:\n",
    "        if cnt > 1:\n",
    "            return False\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i-1] >= nums[i]:\n",
    "                res_1 = self.findAndDelete(cnt + 1, nums[:i-1] + nums[i:])\n",
    "                res_2 = self.findAndDelete(cnt + 1, nums[:i] + nums[i+1:])\n",
    "                if res_1 or res_2:\n",
    "                    return True\n",
    "                return False\n",
    "        return True\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        return self.findAndDelete(0, nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        def is_increasing(nums: list[int]) -> bool:\n",
    "            for i in range(1, len(nums)):\n",
    "                if nums[i] <= nums[i - 1]:\n",
    "                    return False\n",
    "            return True\n",
    "        from copy import deepcopy\n",
    "        for i, num in enumerate(nums):\n",
    "            increasing_nums = deepcopy(nums)\n",
    "            increasing_nums.pop(i)\n",
    "            if is_increasing(increasing_nums):\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        def isIncerase(list):\n",
    "            if list:\n",
    "                if len(list)>=2:\n",
    "                    for i in range(1,len(list)):\n",
    "                        if list[i-1]>=list[i]:\n",
    "                            return False\n",
    "            return True\n",
    "        \n",
    "        if isIncerase(nums):\n",
    "            return True\n",
    "        else:\n",
    "            for j in range(len(nums)):\n",
    "                nums1 = nums.copy()\n",
    "                nums1.pop(j)\n",
    "                if isIncerase(nums1):\n",
    "                    return True\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        stack = []\n",
    "        i,flag,n = 0,0,len(nums)\n",
    "        while i < n:\n",
    "            # print('nums[i]:',nums[i])\n",
    "            # print('stack:',stack)\n",
    "            if flag >= 2:\n",
    "                return False\n",
    "            if not stack or stack[-1]<nums[i]:\n",
    "                stack.append(nums[i])\n",
    "                i += 1\n",
    "            elif stack[-1]>nums[i]:\n",
    "                if len(stack)>=2:\n",
    "                    if stack[-2]<nums[i]:\n",
    "                        stack.pop()\n",
    "                    else:\n",
    "                        i += 1\n",
    "                else:\n",
    "                    stack.pop()\n",
    "                flag += 1\n",
    "            elif stack[-1] == nums[i]:\n",
    "                stack.pop()\n",
    "                flag += 1\n",
    "        # print('len(stack):',len(stack))\n",
    "        # print('stack:',stack)\n",
    "        # print('len(nums)-1:',len(nums)-1)\n",
    "        if len(stack)>=len(nums)-1:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n , pos = len(nums) , -1\n",
    "        for i in range(1 , n):\n",
    "            if nums[i] <= nums[i - 1]:\n",
    "                if pos == -1:\n",
    "                    pos = i\n",
    "                else:\n",
    "                   return False\n",
    "        if pos == 1 or pos == n - 1 or pos == -1:\n",
    "            return True;\n",
    "        \n",
    "        return nums[pos] > nums[pos - 2] or nums[pos - 1] < nums[pos + 1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        idx = None\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i+1] <= nums[i]:\n",
    "                idx = i\n",
    "                break\n",
    "        if idx is None:\n",
    "            return True\n",
    "        nums1 = nums[:idx] + nums[idx+1:]\n",
    "        nums2 = nums[:idx+1] + nums[idx+2:]\n",
    "        ans1 = ans2 = True\n",
    "        for i in range(len(nums1)-1):\n",
    "            if nums1[i+1] <= nums1[i]:\n",
    "                ans1 = False\n",
    "            if nums2[i+1] <= nums2[i]:\n",
    "                ans2 = False\n",
    "        return ans1 or ans2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        def check(arr):\n",
    "            print(arr)\n",
    "            i = 0\n",
    "            j = 1\n",
    "            if len(arr) == 2:\n",
    "                return True\n",
    "            while(j<=len(arr)-1):\n",
    "               if(nums[arr[j]]-nums[arr[i]]>0):  \n",
    "                   j += 1\n",
    "                   i += 1\n",
    "               else:  \n",
    "                   print('F')\n",
    "                   return False\n",
    "            print('T')       \n",
    "            return True\n",
    "\n",
    "        i = 0\n",
    "        j = 1\n",
    "        if len(nums) == 2:\n",
    "            return True\n",
    "        reverse_list = []\n",
    "        while(j<=len(nums)-1):\n",
    "            if(nums[j]-nums[i]>0):\n",
    "                j += 1\n",
    "                i += 1\n",
    "            else:\n",
    "                reverse_list.append((i, j))\n",
    "                j += 1\n",
    "                i += 1\n",
    "        if len(reverse_list) > 1:\n",
    "            return False\n",
    "        elif  len(reverse_list) == 0:\n",
    "            return True   \n",
    "        sample1 = []    \n",
    "        sample2 = [] \n",
    "        if  reverse_list[0][0] > 0:\n",
    "            sample1.append(reverse_list[0][0]-1)\n",
    "            sample2.append(reverse_list[0][0]-1)\n",
    "        sample1.append(reverse_list[0][0])\n",
    "        sample2.append(reverse_list[0][1])\n",
    "        if  reverse_list[0][1] < len(nums)-1:    \n",
    "            sample1.append(reverse_list[0][1]+1)\n",
    "            sample2.append(reverse_list[0][1]+1)\n",
    "        if check(sample1) or check(sample2):\n",
    "            return True\n",
    "        else:      \n",
    "            return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        # 检查数组 nums 在删去下标为 idx 的元素后是否严格递增\n",
    "        def check(idx: int) -> bool:\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            # 寻找非递增相邻下标对\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        idx = None\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i+1] <= nums[i]:\n",
    "                idx = i\n",
    "                break\n",
    "        if idx is None:\n",
    "            return True\n",
    "        nums1 = nums[:idx] + nums[idx+1:]\n",
    "        nums2 = nums[:idx+1] + nums[idx+2:]\n",
    "        ans1 = ans2 = True\n",
    "        for i in range(len(nums1)-1):\n",
    "            if nums1[i+1] <= nums1[i]:\n",
    "                ans1 = False\n",
    "            if nums2[i+1] <= nums2[i]:\n",
    "                ans2 = False\n",
    "        return ans1 or ans2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        import copy\n",
    "        s=[]\n",
    "        flag=1\n",
    "        for i in range(len(nums)):\n",
    "            s=copy.deepcopy(nums)\n",
    "            s.pop(i)\n",
    "            for j in range(len(s)-1):\n",
    "                if s[j]>=s[j+1]:   \n",
    "                   flag=0\n",
    "                   break\n",
    "                if s[j]<s[j+1]:\n",
    "                   flag=1\n",
    "            if flag==1:\n",
    "               return True\n",
    "        return False\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        idx = None\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i+1] <= nums[i]:\n",
    "                idx = i\n",
    "                break\n",
    "        if idx is None:\n",
    "            return True\n",
    "        nums1 = nums[:idx] + nums[idx+1:]\n",
    "        nums2 = nums[:idx+1] + nums[idx+2:]\n",
    "        ans1 = ans2 = True\n",
    "        for i in range(len(nums1)-1):\n",
    "            if nums1[i+1] <= nums1[i]:\n",
    "                ans1 = False\n",
    "            if nums2[i+1] <= nums2[i]:\n",
    "                ans2 = False\n",
    "        return ans1 or ans2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        if n == len(set(nums)) and nums == sorted(nums):\n",
    "            return True\n",
    "        for i in range(n):\n",
    "            arr = nums[:i]\n",
    "            arr.extend(nums[i+1:])\n",
    "            flag = True\n",
    "            for i in range(n-2):\n",
    "                if arr[i] >= arr[i+1]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag:\n",
    "                return True\n",
    "        \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        last = nums[0]\n",
    "        chance = True\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] <= last:\n",
    "                if not chance:\n",
    "                    return False\n",
    "                chance = False\n",
    "                last = nums[i] if i == 1 or nums[i] > nums[i-2] else nums[i-1]\n",
    "            else:\n",
    "                last = nums[i]\n",
    "        return True\n",
    "                \n",
    "                    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "            fails1 = 0\n",
    "            fails2 = 0\n",
    "            \n",
    "            for i in range(len(nums)-1):\n",
    "                    if nums[i] >= nums[i+1]:\n",
    "                        fails1 = fails1 + 1\n",
    "                        \n",
    "            for i in range(len(nums)-2):\n",
    "                    if nums[i] >= nums[i+2]:\n",
    "                        fails2 = fails2 + 1\n",
    "                        \n",
    "            if (fails1 < 2) and (fails2 < 2):\n",
    "                        \n",
    "                return True\n",
    "            else:\n",
    "                return False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        def check(n):\n",
    "            for i in range(1,len(n)):\n",
    "                if n[i]<=n[i-1]:\n",
    "                    return False\n",
    "\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i-1]>=nums[i]:\n",
    "                return check(nums[:i-1] + nums[i:]) or check(nums[:i] + nums[i+1:])\n",
    "        \n",
    "        return True\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        res = []\n",
    "        for i in range(len(nums)):\n",
    "            a = nums[:i] + nums[i+1:]\n",
    "            # print(a)\n",
    "            for j in range(1,len(a)):\n",
    "                if a[j] <= a[j-1]:\n",
    "                    res.append(0)\n",
    "                    break\n",
    "        # print(res)\n",
    "        return len(res) < len(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        count=0\n",
    "        ind=0\n",
    "        for i in range(1,len(nums)):\n",
    "            if nums[i]<=nums[i-1]:\n",
    "                count+=1\n",
    "                ind=i-1\n",
    "        \n",
    "        if count==0:\n",
    "            return True\n",
    "        if count>1:\n",
    "            return False\n",
    "        if count==1:\n",
    "            if ind==len(nums)-2 or ind==0 or nums[ind+1]>nums[ind-1] or (ind+2<len(nums) and nums[ind]<nums[ind+2]):\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                break\n",
    "        nums2 = nums.copy()\n",
    "        nums.pop(i)\n",
    "        nums2.pop(i-1)\n",
    "        return nums == sorted(list(set(nums))) or nums2 == sorted(list(set(nums2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        c, i, j = 0, 0, 1\n",
    "        while j < len(nums):\n",
    "            if nums[j] > nums[i]:\n",
    "                i = j\n",
    "                j += 1\n",
    "            else:\n",
    "                c += 1\n",
    "                if i > 0:\n",
    "                    if nums[j] > nums[i - 1]:\n",
    "                        i -= 1\n",
    "                    else:\n",
    "                        j += 1\n",
    "                else:\n",
    "                    i = j\n",
    "                    j += 1\n",
    "            if c > 1:\n",
    "                return False\n",
    "        return c <= 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        cnt = 0\n",
    "        for i in range(1,n):\n",
    "            if nums[i]<=nums[i-1]:\n",
    "                cnt+=1\n",
    "                if i-2>=0:\n",
    "                    if nums[i-2]>=nums[i]:\n",
    "                        nums[i] = nums[i-1]\n",
    "        if cnt>=2:\n",
    "            return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def check(self, nums):\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i+1]<=nums[i]:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        for i in range(len(nums)):\n",
    "            new_nums = nums[:i]+nums[i+1:]\n",
    "            if self.check(new_nums) is True:\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        def check(i: int) -> bool:\n",
    "            return all(x < y for x, y in pairwise(nums[:i] + nums[i + 1:]))\n",
    "        return any(check(i) for i in range(len(nums) + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        for i in range(len(nums)):\n",
    "            if sorted(nums[:i]+nums[i+1:])==nums[:i]+nums[i+1:] and len(set(nums[:i]+nums[i+1:]))==len(nums)-1:return True\n",
    "        return False\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n=len(nums)\n",
    "        for i in range(n):\n",
    "            new=nums[:i]+nums[i+1:]\n",
    "            for j in range(1,n-1):\n",
    "                if new[j-1]>=new[j]: break\n",
    "            else: return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        for i in range(len(nums)):\n",
    "            if sorted(nums[:i] + nums[i+1:]) == nums[:i] + nums[i+1:] and len(set(nums[:i] + nums[i+1:])) == len(nums) - 1:\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        if len(nums)-len(set(nums))>=2:\n",
    "            return False\n",
    "        nums_sort=sorted(nums,reverse=False)\n",
    "        if nums_sort==nums:\n",
    "            return True\n",
    "        one=nums.copy()\n",
    "        two=nums.copy()\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i]>=nums[i+1]:\n",
    "                one.pop(i)\n",
    "                two.pop(i+1)\n",
    "                break\n",
    "        three=sorted(one,reverse=False)\n",
    "        four=sorted(two,reverse=False)\n",
    "        if one==three and len(one)==len(set(three)):\n",
    "            return True\n",
    "        if two==four and len(two)==len(set(four)):\n",
    "            return True\n",
    "        return False\n",
    "        \n",
    "        \n",
    "\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hh(self,nums):\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i]>=nums[i+1]:\n",
    "                return False\n",
    "        return True\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n=len(nums)\n",
    "        for i in range(n):\n",
    "            tt=nums[0:i]+nums[i+1:]\n",
    "            if self.hh(tt):\n",
    "                return True\n",
    "        return False\n",
    "        \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: list) -> bool:\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            nums1 =nums[:]\n",
    "            del nums1[i]\n",
    "            nums2 = sorted(nums1)\n",
    "            #set 的作用：创建一个无需不重复的元素集，重复的都会被删掉\n",
    "            if nums2 == nums1 and len(set(nums2)) == len(nums2):\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        for i in range(n - 1):\n",
    "            if nums[i + 1] <= nums[i]:\n",
    "                a = nums.pop(i)\n",
    "                if all(nums[j] > nums[j - 1] for j in range(max(i - 1, 1), n - 1)):\n",
    "                    return True\n",
    "                nums[i] = a\n",
    "                if all(nums[j] > nums[j - 1] for j in range(max(i - 1, 1), n - 1)):\n",
    "                    return True\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        def isIncerase(list):\n",
    "            if list:\n",
    "                if len(list)>=2:\n",
    "                    for i in range(1,len(list)):\n",
    "                        if list[i-1]>=list[i]:\n",
    "                            return False\n",
    "            return True\n",
    "        \n",
    "        if isIncerase(nums):\n",
    "            return True\n",
    "        else:\n",
    "            for j in range(len(nums)):\n",
    "                nums1 = nums.copy()\n",
    "                nums1.pop(j)\n",
    "                if isIncerase(nums1):\n",
    "                    return True\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        # 检查数组 nums 在删去下标为 idx 的元素后是否严格递增\n",
    "        def check(idx: int) -> bool:\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            # 寻找非递增相邻下标对\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        index = 0\n",
    "        while index < len(nums):\n",
    "            temp_nums = copy.copy(nums)\n",
    "            temp_nums.pop(index)\n",
    "            if temp_nums == sorted(list(set(temp_nums))):\n",
    "                return True\n",
    "            index += 1\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "### 89/109---pass (my：双指针)\n",
    "# class Solution:\n",
    "#     def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "#         cnt=0\n",
    "#         for i,c in enumerate(nums):\n",
    "#             if i>0 and nums[i-1]>=c:\n",
    "#                 if cnt>0:return False\n",
    "#                 j=i-1\n",
    "#                 while j>=0 and nums[j]>=c:\n",
    "#                     j-=1\n",
    "#                 cnt+=i-j-1\n",
    "#                 if cnt>1:return False \n",
    "#         return True\n",
    "\n",
    "# class Solution:\n",
    "#     def canBeIncreasing(self,nums):\n",
    "#         n=len(nums)\n",
    "#         ### 从pre，cur索引开始确认nums是否单调递增\n",
    "#         def check(pre,cur):\n",
    "#             for i in range(cur,n):\n",
    "#                 if nums[i]<=nums[pre]:\n",
    "#                     return False \n",
    "#                 pre=i\n",
    "#             return True  \n",
    "#         for i in range(1,n):\n",
    "#             ###找到第一个不符合递增序列的元素\n",
    "#             if nums[i]<=nums[i-1]:\n",
    "#                 p1,p2=False,False ### 初始化p1,p2\n",
    "#                 if i-1>=0 and i+1<n:\n",
    "#                     p1=check(i-1,i+1)\n",
    "#                 if i-2>=0:\n",
    "#                     p2=check(i-2,i)    \n",
    "#                 return p1 or p2 \n",
    "#         return True\n",
    "            \n",
    "### 官：\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        # 检查数组 nums 在删去下标为 idx 的元素后是否严格递增\n",
    "        def check(idx: int) -> bool:\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            # 寻找非递增相邻下标对\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        for i in range(len(nums)):\n",
    "            s=nums[::]\n",
    "            s.pop(i)\n",
    "            t=sorted(s)\n",
    "            if s==t and len(s)==len(set(s)):\n",
    "                return True\n",
    "                break\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        break_points = []\n",
    "        length = len(nums)\n",
    "        for i in range(1, length):\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                break_points.append(i-1)\n",
    "                break_points.append(i)\n",
    "                break\n",
    "        else:\n",
    "            return True\n",
    "\n",
    "        for break_point in break_points:\n",
    "            num_copy = nums.copy()\n",
    "            num_copy.pop(break_point)\n",
    "            for i in range(1, length-1):\n",
    "                if num_copy[i] <= num_copy[i-1]:\n",
    "                    break\n",
    "            else:\n",
    "                return True\n",
    "        else:\n",
    "            return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        for i in range(len(nums)):\n",
    "            new_nums = nums[:]\n",
    "            del new_nums[i]\n",
    "            new_nums2 = sorted(new_nums)\n",
    "            if new_nums == new_nums2 and len(set(new_nums2)) == len(new_nums2):\n",
    "                return True\n",
    "        return False\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        # 检查数组 nums 在删去下标为 idx 的元素后是否严格递增\n",
    "        def check(idx: int) -> bool:\n",
    "            for i in range(1, n - 1):\n",
    "                prev, curr = i - 1, i\n",
    "                if prev >= idx:\n",
    "                    prev += 1\n",
    "                if curr >= idx:\n",
    "                    curr += 1\n",
    "                if nums[curr] <= nums[prev]:\n",
    "                    return False\n",
    "            return True\n",
    "        \n",
    "        for i in range(1, n):\n",
    "            # 寻找非递增相邻下标对\n",
    "            if nums[i] <= nums[i-1]:\n",
    "                return check(i) or check(i - 1)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        def isIncerase(list):\n",
    "            num = 0\n",
    "            if list:\n",
    "                if len(list)>2:\n",
    "                    for i in range(len(list)-1):\n",
    "                        if list[i]>=list[i+1]:\n",
    "                            if num>=1 :\n",
    "                                return False\n",
    "                            else:\n",
    "                                num += 1\n",
    "                                if i>0 and i+1<len(list) and list[i-1] < list[i+1]:# 删除自己\n",
    "                                    continue\n",
    "                                elif i+2 <len(list) and list[i]<list[i+2]:# 删除下一个\n",
    "                                    continue\n",
    "                                elif i==0 or i == len(list)-2:# 当是第一个元素是，删除自己；当是倒数第二时，删除下一个\n",
    "                                    continue\n",
    "                                else:\n",
    "                                    return False\n",
    "            return True\n",
    "        \n",
    "        return isIncerase(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        pos = -1\n",
    "        for i in range(1, n):\n",
    "            if nums[i] <= nums[i - 1]:\n",
    "                if pos != -1:\n",
    "                    return False\n",
    "                pos = i \n",
    "        return pos <= 1 or nums[pos] > nums[pos - 2] or pos + 1 >= n or nums[pos + 1] > nums[pos - 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            arr = nums[:i]\n",
    "            arr.extend(nums[i+1:])\n",
    "            flag = True\n",
    "            for i in range(n-2):\n",
    "                if arr[i] >= arr[i+1]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag:\n",
    "                return True\n",
    "        \n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n=len(nums)\n",
    "        for i in range(n):\n",
    "            nums1=nums[:]\n",
    "            del nums1[i]\n",
    "            nums2=sorted(nums1)\n",
    "            if nums2==nums1 and len(set(nums2))==len(nums2):\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        nums.append(1005)\n",
    "        p = -1\n",
    "        for i in range(1, n):\n",
    "            if nums[i] <= nums[i - 1]:\n",
    "                p = i\n",
    "                break\n",
    "        if p == -1:\n",
    "            return True\n",
    "        # 删除 nums[p]\n",
    "        res = True\n",
    "        t, nums[p] = nums[p], nums[p - 1]\n",
    "        for i in range(p + 1, n):\n",
    "            if nums[i] <= nums[i - 1]:\n",
    "                if i > p + 1:\n",
    "                    return False\n",
    "                res = False\n",
    "        nums[p] = t\n",
    "        if res:\n",
    "            return res\n",
    "        # 删除 nums[p - 1]\n",
    "        nums[p - 1] = -1 if p == 1 else nums[p - 2]\n",
    "        return nums[p] > nums[p - 1] and nums[p + 1] > nums[p]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        if all(nums[i] > nums[i-1] for i in range(1,len(nums))):\n",
    "            return True\n",
    "        for i in range(len(nums)):\n",
    "            t = nums.copy()\n",
    "            del t[i]\n",
    "            if all(t[idx] > t[idx-1] for idx in range(1,len(t))):\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        def check(index: int) -> bool:\n",
    "            tmp = nums[:index] + nums[index + 1:]\n",
    "            for i in range(n - 2):\n",
    "                if tmp[i] >= tmp[i + 1]:\n",
    "                    return False\n",
    "            return True\n",
    "        for i in range(n - 1):\n",
    "            if nums[i] >= nums[i + 1]:\n",
    "                return check(i) or check(i + 1)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        def f(h):\n",
    "            for i in range(1, len(h)):\n",
    "                if h[i] <= h[i - 1]:\n",
    "                    return False\n",
    "            return True\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] <= nums[i - 1]:\n",
    "                return f(nums[:i-1] + nums[i:]) or f(nums[:i] + nums[i + 1:])\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def canBeIncreasing(self, nums: List[int]) -> bool:\n",
    "        res = []\n",
    "        n = len(nums)\n",
    "        for i in range(1,n):\n",
    "            if nums[i]<=nums[i-1]:\n",
    "                res.append(i)\n",
    "        if len(res)>1:\n",
    "            return False\n",
    "        if len(res)==0:\n",
    "            return True\n",
    "        print(nums, res)\n",
    "        pos= res[0]\n",
    "        if pos ==1 or pos==n-1 or (nums[pos+1]>nums[pos-1]) or (nums[pos]>nums[pos-2]):\n",
    "            return True\n",
    "        return False"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
